<?php
namespace Admin\Controller;
use Think\Controller;
class NoticeController extends Controller {
    public function NoticeList(){

        // $m_id = $_GET['m_id'];

        $noticesModel = M('notices');
        $notices = $noticesModel -> order("n_id")->select();
         $this->index=$notices;
        
        $this->display();
    
    }

    public function delnotice($n_id){
    $noticesModel=M('notices'); 
    //where实在构建条件
    if (false!==$noticesModel->where("n_id=$n_id"/*必须是双引号*/)->delete()) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }

    }

    //添加
     public function addNotice(){
    $this->display();
  }
   public function doaddNotice(){
        $data=$_POST;
        $file=$_FILES;
        //dump($file['photo']['size']);
        
        //没有头像
        $noticesModel = M('notices'); // 实例化User模型
        $createresult=$noticesModel->create();   // 根据表单提交的POST数据创建数据对象
          if ($createresult) {
            $noticesModel->n_date =  date("Y-m-d H:i:s");// 设置用户的创建时
            if ($file['photo']['size']==0) {//没有上传轮播图片
                        $noticesModel->photo= "nopic.jpg"; // 用户不上传头像默认nopic.jpg
                    }else{//有图片
                            $info=$this->MyUpload();
                            $noticesModel->photo = $info['photo']['savename']; // 保存上传的照片根据需要自行组装
                          }
                 $addresult=$noticesModel->add(); // 写入用户数据到数据库
                     if (false !== $addresult)
                             {
                            $this->success('添加成功！', '../Notice/NoticeList');
                             }else{ 
                                     $this->error('数据错误'); 
                                   }

        }else{
                $this->error($noticesModel->getError());  
              }

    
    
    }
    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Public/Upload/'; // 设置附件上传根目录  //  
        $upload->autoSub = false;//关闭子目录，默认true
        $upload->savePath  =     ''; // 设置附件上传(子)目录，默认目录名为年月日
        $info   =   $upload->upload();  // 上传文件 
        if(!$info) {
            $this->error($upload->getError());    
        }
        else{
            return $info;
        }                    
    }

}
?>